﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Model_SSResourceManager
{
    public class MessageManager
    {
        private readonly IDataBase _dataBase;

        internal MessageManager(IDataBase dataBase)
        {
            this._dataBase = dataBase;
        }

        public Message Create(Guid _userId, String _messageTxt)
        {
            Message _message = new Message()
                {
                    Id=Guid.NewGuid(),
                    CreateTime=DateTime.Now,
                    UserId=_userId,
                    MessageText=_messageTxt
                };

            return _message;
        }

        public bool Add(Message _message)
        {
            return _dataBase.AddMessage(_message);
        }

        public bool Update(Message _message)
        {
            return _dataBase.UpdateMessage(_message);
        }

        public bool Delete(Guid _id)
        {
            return _dataBase.DeleteMessage(_id);
        }

        public Message Get(Guid _id)
        {
            return _dataBase.GetMessage(_id);
        }

        public List<Message> GetAll(bool _notSendedOnly=false)
        {
            return _dataBase.GetAllMessage(_notSendedOnly);
        }

        public List<Message> GetActiveByUser(Guid _userId)
        {
            return _dataBase.GetActiveMessageByUser(_userId );
        }

        public void SendMessage(Message _message)
        {
            User _curUser = _dataBase.GetUser(_message.UserId);
            String MsgArg, usMsg;

            if (_curUser.Id == _message.UserId)
            {
                usMsg = "message to " + _curUser.Login.Trim()+" is";
                MessageSender _msgSender = new MessageSender();
                if ( !_message.EmailOk )
                {
                    if (_curUser.Email.Trim() != String.Empty)
                    {
                        MsgArg = usMsg;
                        _message.EmailOk = _msgSender.SendToEmail(_curUser.Email, _message.MessageText);
                        if (_message.EmailOk)                        
                            _message.SendedEmail = DateTime.Now;
                        else
                            MsgArg += " not";

                        MsgArg ="E-mail " + MsgArg +" sended";
                        OnRaiceMessageEvent(new MessageEventArgs(MsgArg));
                    }
                    else
                        _message.EmailOk = true;
                }

                if (!_message.SkypeOk)
                {
                    if (_curUser.Skype.Trim() != String.Empty)
                    {
                        MsgArg = usMsg;
                        _message.SkypeOk = _msgSender.SendToSkype(_curUser.Skype, _message.MessageText);
                        if (_message.SkypeOk)                        
                            _message.SendedSkype = DateTime.Now;                            
                        else
                            MsgArg += " not";

                        MsgArg = "Skype " + MsgArg + " sended";
                        OnRaiceMessageEvent(new MessageEventArgs(MsgArg));
                    }
                    else
                        _message.SkypeOk = true;                    
                }

                this.Update(_message);
            }
        }

        #region Events
        public event EventHandler<MessageEventArgs> MessageEvent;

        private void OnRaiceMessageEvent(MessageEventArgs e)
        {
            EventHandler<MessageEventArgs> handler = MessageEvent;
            if (handler != null)
            {
                handler(this, e);
            }
        }
        #endregion
    }
 
}
